<script setup lang="ts">
import { onMounted, ref } from 'vue';
import Header from './components/Header.vue';
import { getSessionList } from '@/api/message';
import { SessionInfo } from '@/api/message/type';
import { router } from '@/router';

const list = ref([] as SessionInfo[])

onMounted(() => {
    getSessionList().then((res) => {
        list.value = res.data as SessionInfo[];
    });
})

const toChat = (receiverId: string) => {
    router.push({
        name: "MessageChat",
        query: {
            receiverId: receiverId
        }
    });
}

</script>

<template>
    <div class="container">
        <Header class="border"></Header>
        <van-list class="content">
            <div v-for="(item, index) in list" class="list">
                <div class="row" @click="toChat(item.receiver.id)">
                    <van-image round width="60" height="60"
                        :src="item.receiver.avatar"
                    />
                    <div class="right">
                        <div class="top">
                            <p style="margin: 0%;">{{ item.receiver.nickname }}</p>
                            <p style="margin: 0%;">{{ item.latestMessage.sendTime }}</p>
                        </div>
                        <p class="text">{{ item.latestMessage.content }}</p>
                        <div class="badge" v-if="item.unreadCounts > 0">{{ item.unreadCounts }}</div>
                    </div>
                </div>
                <div class="border" v-if="index != list.length - 1"></div>
            </div>
        </van-list>
    </div>
</template>

<style lang="scss" scoped>
.border {
    border-bottom: 1px rgba(22, 21, 21, 0.171) solid;
}
.container {
    align-items: center;
    .content {
        width: 95%;
        background-color: white;
        border-radius: 15px;
        margin-top: 3%;
        .row {
            padding: 1%;
            display: flex;
            padding-left: 3%;
            padding-right: 3%;
            .right {
                display: flex;
                position: relative;
                flex-direction: column;
                width: 80%;
                padding: 3%;
                justify-content: center;
                .top {
                    display: flex;
                    justify-content: space-between;
                }
                .text {
                    margin: 0%; 
                    font-size: 14px; 
                    color: rgba(125, 127, 129, 0.527);
                }
                .badge {
                    background-color: #ff4d4f;
                    color: white;
                    border-radius: 50%;
                    width: 20px;
                    height: 20px;
                    text-align: center;
                    line-height: 20px;
                    position: absolute;
                    right: 5%;
                    bottom: 20%;
                }
            }
        }
    }
}
</style>